package com.gitee.free.coding.core.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.gitee.free.coding.core.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

/**
 * 自动填充类
 * 注意：自动填充只会在使用 MyBatis-Plus 提供的通用 CRUD 方法时生效，例如 insert、updateById 等
 */
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("开始插入填充...");
        Integer currentDate = DateUtils.getCurrentDate();
        Integer currentTime = DateUtils.getCurrentTime();
        this.strictInsertFill(metaObject, "createdAtDate", Integer.class, currentDate);
        this.strictInsertFill(metaObject, "createdAtTime", Integer.class, currentTime);
        this.strictInsertFill(metaObject, "updatedAtDate", Integer.class, currentDate);
        this.strictInsertFill(metaObject, "updatedAtTime", Integer.class, currentTime);
        this.strictInsertFill(metaObject, "deleted", Boolean.class, false);
        log.info("结束插入填充...");
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("开始更新填充...");
        // this.strictInsertFill(metaObject, "updatedBy", String.class, "system");
        this.strictInsertFill(metaObject, "updatedAtDate", Integer.class, DateUtils.getCurrentDate());
        this.strictInsertFill(metaObject, "updatedAtTime", Integer.class, DateUtils.getCurrentTime());
        log.info("结束更新填充...");
    }
}